package com.eclink.asset.consume.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eclink.asset.consume.domain.dao.ConsumeInventory;
import com.eclink.asset.consume.domain.dao.ConsumeInventoryBatch;

import com.eclink.asset.consume.domain.dto.ConsumeInventoryQueryDTO;

import com.eclink.asset.consume.domain.query.ConsumeInventoryCheckAddQueryRangeForm;
import com.eclink.asset.consume.mapper.ConsumeInventoryBatchMapper;
import com.eclink.asset.consume.mapper.ConsumeInventoryMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 资产清单表Service业务层处理
 *
 * @author chenshixian
 */
@Service
public class ConsumeInventoryService {

    @Resource
    private ConsumeInventoryMapper consumeInventoryMapper;

    @Resource
    private ConsumeInventoryBatchMapper consumeInventoryBatchMapper;

    @Resource
    private ConsumeCategoryService consumeCategoryService;

    @Resource
    private ConsumeWarehouseService consumeWarehouseService;

    /**
     * 获取列表
     *
     * @param query 请求
     * @return 资产清单表
     */
    public Page<ConsumeInventory> getInventoryList(ConsumeInventoryQueryDTO query, Pageable pageable) {

        Page<ConsumeInventory> page = new Page<>(pageable.getPageNumber(), pageable.getPageSize());
        page = consumeInventoryMapper.getList(page, query);
        List<ConsumeInventory> list = page.getRecords();
        for (ConsumeInventory item : list) {
            if (item.getCategoryId() != null && item.getCategoryId() > 0) {
                item.setCategoryName(consumeCategoryService.selectNameById(item.getCategoryId()));
            }
            if (item.getWarehouseId() != null && item.getWarehouseId() > 0) {
                item.setWarehouseName(consumeWarehouseService.getWarehouseNameById(item.getWarehouseId()));
            }
        }
        return page;
    }

    /**
     * 获取列表
     *
     * @param fileId 档案ID
     * @return 资产清单表
     */
    public List<ConsumeInventoryBatch> getInventoryBatchList(Long fileId) {

        LambdaQueryWrapper<ConsumeInventoryBatch> qw = new LambdaQueryWrapper<>();
        qw.like(ConsumeInventoryBatch::getFileId, fileId);
        return consumeInventoryBatchMapper.selectList(qw);
    }

    public void updateBatchNum(Long id, Integer num) {
        LambdaUpdateWrapper<ConsumeInventoryBatch> qw = new LambdaUpdateWrapper<>();
        qw.set(ConsumeInventoryBatch::getNum, num);
        qw.eq(ConsumeInventoryBatch::getId, id);
        consumeInventoryBatchMapper.update(null, qw);
    }

    /**
     * 获取列表
     *
     * @param baseForm 请求
     * @return 资产清单表
     */
    public List<ConsumeInventory> getInventoryBatchList(ConsumeInventoryCheckAddQueryRangeForm baseForm) {

        List list = consumeInventoryBatchMapper.selectList(null);
        return list;
    }

    public List<ConsumeInventoryBatch> selectBatchListByFileId(Long fileId) {
        QueryWrapper<ConsumeInventoryBatch> qw = new QueryWrapper<>();
        qw.eq("file_id", fileId);
        qw.gt("num", 0);

        return consumeInventoryBatchMapper.selectList(qw);
    }

    public void updateBatch(ConsumeInventoryBatch batch) {
        consumeInventoryBatchMapper.updateById(batch);
    }

    @Transactional
    public void insertBatch(List<ConsumeInventoryBatch> list) {
        for(ConsumeInventoryBatch batch : list) {
            consumeInventoryBatchMapper.insert(batch);
        }
    }


}
